package cn.source.web.controller.api;

import cn.source.common.core.controller.BaseController;
import cn.source.common.utils.http.HttpUtils;
import cn.source.system.domain.HouseCity;
import cn.source.system.domain.HouseRegion;
import cn.source.system.service.IHouseCityService;
import cn.source.system.service.IHouseRegionService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import reactor.util.annotation.Nullable;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ProvinceController extends BaseController {

    @Resource
    private IHouseCityService houseCityService;

    @Resource
    private IHouseRegionService houseRegionService;

    /** 获取城市id
     * 爬虫爬取贝壳地图 https://map.ke.com/map/110000/ZF
     */
    public void getProvinceBySpider() {
        Integer cityId = 110000;
        String url = "https://map.ke.com/proxyApi/i.c-pc-webapi.ke.com/map/initdata?cityId=" + cityId + "&dataSource=ZF";
        String resp = HttpUtils.sendGet(url);
        JSONObject result = JSON.parseObject(resp);
        //返回成功
        if (result.getInteger("errno") == 0) {
            //爬取城市id
            JSONArray cityList = result.getJSONObject("data").getJSONArray("cityList");

            for (int i = 0; i < cityList.size(); i++) {

                JSONArray topList = cityList.getJSONObject(i).getJSONArray("list");
                for (int k = 0; k < topList.size(); k++) {
                    JSONObject listItem = topList.getJSONObject(k);
                    //设置省份
                    HouseCity houseCity = new HouseCity();
                    houseCity.setLetter(listItem.getString("char"));
                    houseCity.setCityName(listItem.getString("name").substring(0,listItem.getString("name").length()-1));
                    houseCity.setCityId(0);
                    houseCity.setPid(0L);
                    houseCityService.insertCity(houseCity);

                    JSONArray list = listItem.getJSONArray("list");

                    for (int j = 0;  j< list.size(); j++) {
                        JSONObject item = list.getJSONObject(j);
                        System.out.println("item:"+item.getString("name"));
                        HouseCity house = new HouseCity();
                        house.setCityName(item.getString("name"));
                        house.setCityId(item.getInteger("id"));
                        house.setPid(houseCity.getId());
                        houseCityService.insertCity(house);
                    }
                }


            }
        }
    }

    //根据cityId爬取地区列表和经纬度
    public void getRegionByCity(){
        List<HouseCity> cityList = houseCityService.getCityList();
        for (HouseCity houseCity : cityList) {
            String url = "https://map.ke.com/proxyApi/i.c-pc-webapi.ke.com/map/initdata?cityId=" + houseCity.getCityId() + "&dataSource=ZF";
            String resp = HttpUtils.sendGet(url);
            JSONObject result = JSON.parseObject(resp);
            Integer errno = result.getInteger("errno");
            //返回成功
            if (errno == 0) {
                JSONArray optionsList = result.getJSONObject("data").getJSONObject("filters").getJSONObject("region").getJSONArray("options");
                for (int i = 1; i < optionsList.size(); i++) {
                   JSONObject options = optionsList.getJSONObject(i);
                   //插入区域数据
                    HouseRegion houseDistrict= insertRegion(options,0L,houseCity.getCityId());

                    //获取区域的最小单位
                    JSONArray optionsArr = options.getJSONArray("options");
                    for (int j = 1; j < optionsArr.size(); j++) {
                        JSONObject optionItem = optionsArr.getJSONObject(j);
                        //区域的最小单位数据
                        insertRegion(optionItem,houseDistrict.getId(),houseCity.getCityId());
                    }
                }
                System.out.println("======================爬取完成了！！！=========================");
            }else {
                logger.error("请求失败！");
            }
        }


    }

    public HouseRegion insertRegion(JSONObject options,long pid,long cityId){
        HouseRegion houseRegion = new HouseRegion();
        houseRegion.setLatLong(options.getString("border"));
        houseRegion.setRegionName(options.getString("name"));
        houseRegion.setCityId(cityId);
        houseRegion.setRegionId(options.getString("id"));
        houseRegion.setPointLat(options.getDouble("pointLat"));
        houseRegion.setPointLng(options.getDouble("pointLng"));
        houseRegion.setPid(pid);
        //插入区域
        houseRegionService.insertRegion(houseRegion);
        return houseRegion;
    }


    /**
     * 116.450781,30.303747;116.461362,30.300705;116.468706,30.278601;116.484016,30.274735;116.49863,30.262554;116.486176,30.252972;116.482226,30.23692;116.493235,30.206275;116.487157,30.192202;116.468081,30.173843;116.456421,30.165658;116.445387,30.162725;116.434533,30.163505;116.415105,30.182934;116.385668,30.1929;116.383837,30.236674;116.377251,30.243481;116.362695,30.250261;116.352657,30.262238;116.347216,30.275562;116.333731,30.287009;116.315825,30.296884;116.30669,30.281708;116.292293,30.282639;116.284316,30.297194;116.265409,30.311815;116.264135,30.318903;116.255758,30.327283;116.25175,30.353812;116.255022,30.362886;116.236046,30.376348;116.210733,30.381452;116.209584,30.390819;116.191465,30.397726;116.149023,30.394555;116.145527,30.397134;116.144568,30.407901;116.140884,30.407773;116.123077,30.403997;116.114693,30.392999;116.102656,30.394199;116.095998,30.401683;116.082977,30.387073;116.068415,30.384344;116.061921,30.395534;116.046216,30.401636;116.04557,30.412064;116.037036,30.420515;116.011286,30.427528;116.00934,30.432524;115.990278,30.433504;115.974302,30.429169;115.925999,30.436011;115.922408,30.448996;115.902315,30.456884;115.899297,30.465305;115.906101,30.472615;115.903385,30.488223;115.908727,30.500886;115.915055,30.512749;115.926697,30.523412;115.916036,30.525154;115.904693,30.543706;115.89181,30.549389;115.888666,30.564918;115.880954,30.574867;115.884593,30.583824;115.880346,30.588913;115.862673,30.595031;115.854061,30.607463;115.833386,30.611926;115.829461,30.602055;115.824007,30.604475;115.813087,30.621712;115.820385,30.630732;115.817896,30.64497;115.804294,30.656048;115.801175,30.665835;115.780638,30.674777;115.777794,30.683605;115.768171,30.690951;115.772657,30.700282;115.771688,30.715526;115.792347,30.763;115.816104,30.765256;115.82886,30.756294;115.840658,30.762252;115.857064,30.762417;115.858381,30.744855;115.852721,30.737037;115.867281,30.729481;115.867262,30.710545;115.879356,30.700222;115.896445,30.699461;115.904669,30.702335;115.906811,30.687635;115.93231,30.686584;115.947775,30.695835;115.956541,30.69223;115.978394,30.698635;115.980807,30.695595;115.986435,30.701351;115.998701,30.706153;116.008538,30.677732;116.023835,30.689634;116.065873,30.685503;116.08132,30.679172;116.088644,30.680971;116.101572,30.676583;116.105531,30.682148;116.118748,30.687072;116.123788,30.682782;116.13104,30.683945;116.133831,30.665021;116.163121,30.663728;116.176199,30.672347;116.197457,30.670885;116.226162,30.679852;116.230945,30.676537;116.236368,30.659604;116.257866,30.662539;116.264511,30.649477;116.279073,30.644867;116.297163,30.632153;116.303781,30.614131;116.322457,30.612487;116.328003,30.601451;116.357855,30.597939;116.36282,30.594855;116.386537,30.608218;116.390372,30.595319;116.400563,30.595719;116.435035,30.583833;116.4359,30.579162;116.446477,30.571919;116.43765,30.559458;116.437206,30.5503;116.440632,30.525749;116.447488,30.509685;116.455314,30.501419;116.445956,30.488252;116.449069,30.474774;116.483798,30.468037;116.490632,30.454539;116.486766,30.444736;116.487432,30.433904;116.50479,30.403371;116.503609,30.396818;116.497046,30.392076;116.497702,30.386006;116.486742,30.377354;116.472278,30.377695;116.466608,30.371324;116.462378,30.353222;116.464285,30.345984;116.44769,30.340945
     * 树木岭、新开铺、高桥
     * https://map.ke.com/proxyApi/i.c-pc-webapi.ke.com/map/bubblelist?cityId=430100&dataSource=ZF&condition=&id=&groupType=bizcircle&maxLatitude=28.190141083049728&minLatitude=28.131798211180783&maxLongitude=113.161876726287&minLongitude=112.88591727371293
     */

}
